home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / flying-6.11 / logfile.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-30  |  2.0 KB  |  96 lines

  1. #ifndef _global_h
  2. #    include "global.h"
  3. #endif
  4. #ifndef _logfile_h
  5. #    include "logfile.h"
  6. #endif
  7. #ifndef USE_EXTERNAL_STANDARD_INCLUDES
  8. #    include <stdio.h>
  9. #endif
  10.  
  11. #include <errno.h>
  12. #include <unistd.h>
  13.  
  14. static char        message[200];
  15. static time_t    start_time;
  16.  
  17. void end_log() {
  18. FILE        *fp1,*fp2;
  19. char        buffer[200];
  20. time_t    end_time;
  21. // char        new_message[200];
  22. double    dt;
  23. int        hh, mm;
  24.  
  25.     time(&end_time);
  26.     dt = end_time - start_time;
  27.     mm = (int)(dt/60);
  28.     hh = mm/60;
  29.     mm = mm%60;
  30.  
  31.     if ( rename( LOG_FILE, LOG_FILE".BAK" ) ) {
  32.         perror( LOG_FILE );
  33.     }
  34.     fp1 = fopen( LOG_FILE, "w" );
  35.     if (!fp1)    return;
  36.  
  37.     fp2 = fopen( LOG_FILE".BAK", "r" );
  38.     if (fp2) {
  39.         while ( fgets( buffer, sizeof(buffer), fp2 ) ) {
  40.             if ( !strcmp(buffer, message) ) {
  41.                 message[strlen(message)-1]='\0';
  42.                 if (dt/60>1)
  43.                     fprintf( fp1, "%s (%d:%02d)\n", message, hh, mm );
  44.             }
  45.             else fputs( buffer, fp1 );
  46.         }
  47.         fclose(fp2);
  48.     }
  49.     unlink( LOG_FILE".BAK" );
  50.  
  51.     fclose( fp1 );
  52. }
  53.  
  54. void start_log( const char *game ) {
  55. FILE    *fp1,*fp2;
  56. char    buffer[200];
  57. const char    *user_string = getenv("USER");
  58.     if (!user_string || *user_string=='\0')        user_string = "unknown";
  59. const char    *dpy_string  = getenv( "DISPLAY" );
  60.     if (!dpy_string || *dpy_string=='\0')          dpy_string = "localhost:0.0";
  61.  
  62.     printf( "flying:\n" );
  63.     printf( "\t%s", flying_descriptor_string+4 );
  64.     printf( "\t%s", flying_version_string+4 );
  65.  
  66.     time(&start_time);
  67.     strftime( buffer, sizeof(buffer), "%d-%m-%y %H:%M:%S", localtime(&start_time) );
  68.     sprintf( message, "%-8s%s %s: %s started by ", game, buffer,
  69.             dpy_string,revision );
  70.     if ( !strcmp( user_string, "emshh" ) )        return;
  71.  
  72.     strcat( message, user_string );
  73.     strcat( message, "\n" );
  74.  
  75.     if ( rename( LOG_FILE, LOG_FILE".BAK" ) ) {
  76.         perror( LOG_FILE );
  77.     }
  78.     fp1 = fopen( LOG_FILE, "w" );
  79.     if (!fp1)    return;
  80.     fputs( message, fp1 );
  81.  
  82.     fp2 = fopen( LOG_FILE".BAK", "r" );
  83.     if (fp2) {
  84.         while ( fgets( buffer, sizeof(buffer), fp2 ) ) {
  85.             fputs( buffer, fp1 );
  86.         }
  87.         fclose(fp2);
  88.     }
  89.     unlink( LOG_FILE".BAK" );
  90.  
  91.     fclose( fp1 );
  92.  
  93.     // only for ANSI C, therefor commented out
  94.     // atexit( end_log );
  95. }
  96.